package service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;

import bean.Right;
import bean.Role;
import bean.User;
import dao.RightDao;
import dao.RoleDao;
import dao.UserDao;

public class UserService {
	private UserDao userDao = new UserDao();
	private RightDao rightDao = new RightDao();
	private RoleDao roleDao = new RoleDao();

	public User login(String username) {
		return userDao.getUser(username);
	}

	public boolean register(String username, String md5password) {
		User user = new User();
		final int userNum = userDao.getUserNum()+1;
		user.setUserId(userNum);
		user.setUserName(username);
		user.setUserPassword(md5password);
		return userDao.insertUser(user);
	}

	@SuppressWarnings("unchecked")
	public List<Right> getUserMenu(User user) {
		List<Right> result = new ArrayList<>();
		Iterator<Right> iterator = user.getRights().iterator();
		while (iterator.hasNext()) {
			final Right next = iterator.next();
			result.add(next);
		}
		final Role role = user.getRole();
		iterator = role.getRights().iterator();
		while (iterator.hasNext()) {
			Right right = (Right) iterator.next();
			result.add(right);
		}
		for (Right right : result) {
			System.out.println(right.getRightName());
		}
		return result;
	}

	public boolean modifyUserInfo(User user){
		return userDao.updateUser(user);
	}
}
